﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MapEntities;
using ServerPlatform.Web;
using ServerPlatform.Exceptions;

namespace WebApp.Controls
{
    public partial class GeneralRuleEditor : BaseDataEditor<SilverlightMapProperties>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                foreach (string xmode in Enum.GetNames(typeof(Mode)))
                {
                    ctlBackgroundMode.Items.Add(new ListItem(xmode));
                }
            }
        }

        protected override void ShowEntity(SilverlightMapProperties entity)
        {
            ctlBackgroundMode.SelectedIndex = ctlBackgroundMode.Items.IndexOf(ctlBackgroundMode.Items.FindByText(entity.BackgroundMode.ToString()));
            ctlCopyrightVisible.Checked = entity.CopyrightVisible;
            ctlHeight.Text = entity.Height;
            ctlLatitude.Text = entity.Center.Lat.ToString();
            ctlLongitude.Text = entity.Center.Long.ToString();
            ctlMouseWhellVisible.Checked = entity.MouseWhellZoom;
            ctlScalaVisible.Checked = entity.ScaleVisible;
            ctlShowNavigationControls.Checked = entity.ShowNavigationControls;
            ctlWidth.Text = entity.Width;
            ctlZoomLevel.Text = entity.ZoomLevel.ToString();
            ctlLogoVisible.Checked = entity.LogoVisible;
            ctlBingMapKey.Text = entity.BingMapKeY ?? string.Empty;
        }

        public override SilverlightMapProperties GetEntity()
        {
            Current.BackgroundMode = (Mode)Enum.Parse(typeof(Mode), ctlBackgroundMode.SelectedItem.Text);
            Current.BingMapKeY = ctlBingMapKey.Text.Trim();
            try
            {
                Current.Center.Lat = System.Convert.ToDouble(ctlLatitude.Text.Trim());
                Current.Center.Long = System.Convert.ToDouble(ctlLongitude.Text.Trim());
                Current.ZoomLevel = System.Convert.ToDouble(ctlZoomLevel.Text.Trim());
            }
            catch (FormatException)
            {
                throw new BusinessException("Latitude , Longitude and Zoom Level must be numeric");
            }
            Current.CopyrightVisible = ctlCopyrightVisible.Checked;
            Current.Height = ctlHeight.Text.Trim();
            Current.LogoVisible = ctlLogoVisible.Checked;
            Current.MouseWhellZoom = ctlMouseWhellVisible.Checked;
            Current.ScaleVisible = ctlScalaVisible.Checked;
            Current.ShowNavigationControls = ctlShowNavigationControls.Checked;
            Current.Width = ctlWidth.Text.Trim();
            SaveEntity();
            return Current;
        }
    }
}